//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.list
//
//import com.intellij.collaboration.async.combineAndCollect
//import com.intellij.collaboration.async.launchNow
//import com.intellij.collaboration.messages.CollaborationToolsBundle
//import com.intellij.collaboration.ui.CollaborationToolsUIUtil
//import com.intellij.collaboration.ui.codereview.list.error.ErrorStatusPanelFactory
//import com.intellij.openapi.project.Project
//import com.intellij.ui.SimpleTextAttributes
//import com.intellij.ui.components.panels.Wrapper
//import com.intellij.util.ui.SingleComponentCenteringLayout
//import com.intellij.util.ui.StatusText
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.launch
//import kotlinx.coroutines.yield
//import com.linqingying.gitcode.authentication.accounts.GitCodeAccount
//import com.linqingying.gitcode.i18n.GitCodeBundle
//import com.linqingying.gitcode.pullrequest.GCPRListViewModel
//import com.linqingying.gitcode.pullrequest.ui.filters.GCPRListSearchValue
//import javax.swing.JComponent
//import javax.swing.JPanel
//
//internal class GCPRListPanelController(
//  project: Project,
//  scope: CoroutineScope,
//  private val listVm: GCPRListViewModel,
//  private val emptyText: StatusText,
//  private val listComponent: JComponent,
//  private val mainPanel: Wrapper
//) {
//
//  private val errorPanel: JComponent = createErrorPanel(project, scope, listVm.account)
//
//  init {
//    scope.launch {
//      listVm.error.collect { error ->
//        mainPanel.setContent(if (error != null) errorPanel else listComponent)
//        mainPanel.repaint()
//      }
//    }
//    scope.launch {
//      combineAndCollect(listVm.loading, listVm.searchVm.searchState) { isLoading, searchValue ->
//        updateEmptyText(isLoading, searchValue)
//      }
//    }
//
//    scope.launchNow {
//      listVm.focusRequests.collect {
//        yield()
//        CollaborationToolsUIUtil.focusPanel(mainPanel)
//      }
//    }
//  }
//
//  private fun updateEmptyText(isLoading: Boolean, searchValue: GCPRListSearchValue) {
//    emptyText.clear()
//    if (isLoading) {
//      emptyText.appendText(CollaborationToolsBundle.message("review.list.empty.state.loading"))
//      return
//    }
//
//    if (searchValue.filterCount == 0) {
//      emptyText.appendText(GitCodeBundle.message("pull.request.list.nothing.loaded", listVm.repository))
//    }
//    else {
//      emptyText
//        .appendText(GitCodeBundle.message("pull.request.list.no.matches"))
//        .appendSecondaryText(GitCodeBundle.message("pull.request.list.filters.clear"), SimpleTextAttributes.LINK_ATTRIBUTES) {
//          listVm.searchVm.searchState.value = GCPRListSearchValue.EMPTY
//        }
//    }
//  }
//
//  private fun createErrorPanel(project: Project, scope: CoroutineScope, account: GitCodeAccount): JComponent {
//    val errorPresenter = GCPRErrorStatusPresenter(project, account) {
//      listVm.refresh()
//    }
//    val errorPanel = ErrorStatusPanelFactory.create(scope, listVm.error, errorPresenter)
//    return JPanel(SingleComponentCenteringLayout()).apply {
//      add(errorPanel)
//    }
//  }
//}
